<?php
use yii\helpers\Url;
?>
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>铝邦CRM客户管理系统</title>
    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"/>
    <link rel="stylesheet" href="css/common.css"/>
    <link rel="stylesheet" href="css/public.css"/>
    <link rel="stylesheet" href="css/setting.css"/>
    <script src="js/jquery-1.12.3.min.js"></script>
    <script src="bootstrap/js/bootstrap.min.js"></script>
    <script src="layui/layui.all.js"></script>
</head>
<body class="body-set">
    <div class="main-nav">
        <h3>部门列表</h3>
        <div class="add-bar col-sm-1" data-toggle="modal" data-target="#addBarch">+新增部门</div>
    </div>
    <div class="set-bar-news">
        <p> 提示：1、数据权限是依赖于公司组织架构的：员工只能看到自己负责的线索、客户、代理。</p>
        <p> 上级部门可以看到下级部门员工的所有数据。同一部门的员工可以看到下级的数据，但互相看不到对方自己负责的数据。</p>
        <p> 2、部门组织架构支持多级、最多支持5级。</p>
    </div>
    <div class="set-bar-msg">
        <div class="bar-msg-title">
            <div class="col-sm-2">级别</div>
            <div class="col-sm-6">部门名称</div>
            <div class="col-sm-2">部门人数</div>
            <div class="col-sm-2">操作</div>
        </div>
        <ul class="level-0">
        <?php foreach($departmentsList as $value){

         ?>
            <li class="level-1">
                <div class="col-sm-2">1</div>
                <div class="pid">1</div>
                <div class="col-sm-6"><?=$value['departName']?></div>
                <div class="col-sm-2"><?=$value['departPcount']?></div>
                <div class="col-sm-2"></div>
            </li>
            <?php foreach($value['twoGrade'] as $twoValue){
                ?>
                <li class="level-2">
                    <div class="col-sm-2">----2</div>
                    <div class="pid">11</div>
                    <div class="col-sm-6"><?=$twoValue['departName']?></div>
                    <div class="col-sm-2"><?=$twoValue['departPcount']?></div>
                    <div class="col-sm-2">
                        <span><img src="images/set-bj.png" onclick='toEdit(<?=$twoValue['id']?>,<?=$twoValue['parentId']?>,"<?=$twoValue['departName']?>")'/></span>
                        <span><img class="bar-del" departmentId="<?=$twoValue['id']?>"  src="images/set-del.png"/></span>
                    </div>
                </li>
            <?php
                foreach($twoValue['threeGrade'] as $threeValue){
                    ?>
                    <li class="level-3">
                        <div class="col-sm-2">--------3</div>
                        <div class="pid">121</div>
                        <div class="col-sm-6"><?=$threeValue['departName']?></div>
                        <div class="col-sm-2"><?=$threeValue['departPcount']?></div>
                        <div class="col-sm-2">
                            <span><img src="images/set-bj.png" onclick='toEdit(<?=$threeValue['id']?>,<?=$threeValue['parentId']?>,"<?=$threeValue['departName']?>")'/></span>
                            <span><img class="bar-del" departmentId="<?=$threeValue['id']?>" src="images/set-del.png"/></span>
                        </div>
                    </li>
            <?php


                foreach($threeValue['fourGrade'] as $fourValue){

                    ?>
                    <li class="level-4">
                        <div class="col-sm-2">------------4</div>
                        <div class="pid">1511</div>
                        <div class="col-sm-6"><?=$fourValue['departName']?></div>
                        <div class="col-sm-2"><?=$fourValue['departPcount']?></div>
                        <div class="col-sm-2">
                            <span><img src="images/set-bj.png" onclick='toEdit(<?=$fourValue['id']?>,<?=$fourValue['parentId']?>,"<?=$fourValue['departName']?>")'/></span>
                            <span><img class="bar-del" departmentId="<?=$fourValue['id']?>" src="images/set-del.png"/></span>
                        </div>
                    </li>
               <?php
                    foreach($fourValue['fiveGrade'] as $fiveValue){

                        ?>
                        <li class="level-5">
                            <div class="col-sm-2">----------------5</div>
                            <div class="pid">15111</div>
                            <div class="col-sm-6"><?=$fiveValue['departName']?></div>
                            <div class="col-sm-2"><?=$fiveValue['departPcount']?></div>
                            <div class="col-sm-2">
                                <span><img src="images/set-bj.png" onclick='toEdit(<?=$fiveValue['id']?>,<?=$fiveValue['parentId']?>,"<?=$fiveValue['departName']?>")'/></span>
                                <span><img class="bar-del" departmentId="<?=$fiveValue['id']?>" src="images/set-del.png" onclick=""/></span>
                            </div>
                        </li>
                  <?php } ?>
                <?php
                } ?>
            <?php    } ?>
           <?php } ?>

        <?php } ?>

        </ul>
    </div>
    <!--<script>-->
        <!--$(function () {-->
            <!--$(".level-0 li div:first-child").click(function () {-->
                <!--var lv = $(this).text();-->
                <!--lv = parseInt(lv)+1;-->
                <!--var pid =$(this).siblings(".pid").text();-->
                <!--if(lv == 2){-->
                    <!--$(".level-2").toggle();-->
                    <!--$(".level-3").hide();-->
                    <!--$(".level-4").hide();-->
                    <!--$(".level-5").hide();-->
                <!--}else if(lv == 3){-->
                    <!--$(".level-3").toggle();-->
                    <!--$(".level-4").hide();-->
                    <!--$(".level-5").hide();-->
                <!--}else if(lv == 4){-->
                    <!--$(".level-4").toggle();-->
                    <!--$(".level-5").hide();-->
                <!--}else{-->
                    <!--$(".level-5").toggle();-->
                <!--}-->
            <!--})-->
        <!--})-->
    <!--</script>-->
    <!-- 模态框 新增部门-->
    <div class="modal fade" id="addBarch" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title add-title">新增部门</h4>
                </div>
                <div class="modal-body">
                    <label class="modal-label">上级部门：</label>
                    <select name="" id="theDepartment" class="modal-select">
                        <option value="0">--请选择--</option>
                        <?php foreach($departmentsList as $value){

                            ?>
                            <option value="<?=$value['id']?>"><?=$value['departName']?></option>

                            <?php foreach($value['twoGrade'] as $twoValue){
                                ?>
                                <option value="<?=$twoValue['id']?>">-<?=$twoValue['departName']?></option>
                                <?php
                                foreach($twoValue['threeGrade'] as $threeValue){
                                    ?>
                                    <option value="<?=$threeValue['id']?>">--<?=$threeValue['departName']?></option>
                                    <?php
                                    foreach($threeValue['fourGrade'] as $fourValue){
                                        ?>
                                        <option value="<?=$fourValue['id']?>">---<?=$fourValue['departName']?></option>
                                        <?php
                                        foreach($fourValue['fiveGrade'] as $fiveValue){

                                            ?>

                                            <option value="<?=$fiveValue['id']?>">----<?=$fiveValue['departName']?></option>
                                        <?php } ?>
                                        <?php
                                    } ?>
                                <?php    } ?>
                            <?php } ?>

                        <?php } ?>
                    </select>
                    <label class="modal-label">部门名称：</label>
                    <input type="text" id="departName" placeholder="请输入部门名称" name="addBarch" class="modal-input"/>
                    <input type="hidden" id="theId" value=""/>
                    <p>(<span>0</span>/10)</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary" onclick="toCommit()">保存</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </div>
        </div>
    </div>
    <!--删除-->
    <div class="modal" id="delBarch" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title add-title">提示</h4>
                </div>
                <input type="hidden" id="delId" value=""/>
                <div class="modal-body">
                    <p class="modal-p">确定删除所选部门？该操作成功后，将无法恢复！</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-primary btn-del" >确定删除</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        $(function () {
            $("#addBarch").modal({
                keyboard:false,
                show:false
            });
//            自己写的，没有发请求
            $(".editbarch").click(function () {
                var tt = $(this);
                var txt = $(tt).parents("li").find(".col-sm-6").text();
                $("#editBarch").find("input").val(txt);
                $(tt).attr({"data-toggle":"modal","data-target":"#editBarch"});
                $(".btn-primary").click(function () {
                    var txt = $(this).parents(".modal-content").find("input").val();
                    var ttx = $(tt).parents("li").find(".col-sm-6").text();
                    if(txt == ttx){
                        layer.msg("没有修改");
                    }else{
                        layer.msg("修改成功");
                        $(tt).parents("li").find(".col-sm-6").text(txt);
                        $(this).attr({"data-dismiss":"modal"});
                    }

                })
            });
            $(".bar-del").click(function () {
                var delId=$(this).attr('departmentId');
                var num = $(this).parents("li").find("a").text();
                num = parseInt(num);
               /* if(num !=0){
                    layer.msg("非法操作，不能删除，请优先删除其下部门~~~");
                }else{
                    $(this).attr({"data-toggle":"modal","data-target":"#delBarch"});
                    var tt=$(this);
                    $(".btn-del").click(function () {
                        $(tt).parents("li").remove();
                        layer.msg("删除成功");
                    })
                }*/
                $('#delId').val(delId);
                $(this).attr({"data-toggle":"modal","data-target":"#delBarch"});
                var tt=$(this);
                $(".btn-del").click(function () {
                    var params={};
                    params.departmentId=delId;
                    var url="<?=Url::toRoute(['system/del-department'])?>";
                    $.post(url,params,function(data){
                        var data=JSON.parse(data);
                        if(data.errcode==0){
                            layer.msg(data.errmsg);
                            window.location.reload();
                        }else{
                            layer.msg(data.errmsg);
                        }
                    });
                  //  $(tt).parents("li").remove();

                })
            })
        })
        function toCommit(){
            var params={};
            params.departmentId=$('#theDepartment').val();
            params.departName=$('#departName').val();
            params.theId=$('#theId').val();
            $url="<?=Url::toRoute(['system/add-departmented'])?>";
            $.post($url,params,function(data){
                var data=JSON.parse(data);
                if(data.errcode==0){
                    alert(data.errmsg);
                    window.location.reload();
                }else{
                    alert(data.errmsg);
                }
            });
        }
        function toEdit(theId,departmentId,departName){
           // alert('123');
            $('#theId').val(theId);
            $('#theDepartment').val(departmentId);
            $('#departName').val(departName);
            $("#addBarch").modal({
                keyboard:true,
                show:true
            });
        }
        /**
         * 确认删除部门
         */

    </script>
</body>
</html>